package site.isscloud.web.controller.project;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import site.isscloud.common.core.controller.BaseController;
import site.isscloud.common.core.domain.AjaxResult;
import site.isscloud.common.core.page.TableDataInfo;
import site.isscloud.common.enums.BusinessType;
import site.isscloud.common.annotation.Log;
import site.isscloud.common.utils.poi.ExcelUtil;
import site.isscloud.project.domain.vo.BuildingEventVo;
import site.isscloud.project.domain.PjAlarmEvent;
import site.isscloud.project.service.IPjBuildingEventService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 楼栋告警Controller
 */
@RestController
@RequestMapping("/project/bevent")
public class BuildingEventController extends BaseController {

    @Autowired
    private IPjBuildingEventService buildingEventService;

    /**
     * 查询楼栋告警汇总列表
     */
    @PreAuthorize("@ss.hasPermi('project:bevent:list')")
    @GetMapping("/list")
    public TableDataInfo list(BuildingEventVo buildingEventVo, HttpServletRequest request) {
        // 设置时间范围参数到 params 中
        if (buildingEventVo.getParams() == null) {
            buildingEventVo.setParams(new java.util.HashMap<>());
        }

        // 直接从请求参数中获取时间范围
        String beginTime = request.getParameter("params[beginTime]");
        String endTime = request.getParameter("params[endTime]");

        if (beginTime != null && !beginTime.trim().isEmpty()) {
            buildingEventVo.getParams().put("beginTime", beginTime);
        }
        if (endTime != null && !endTime.trim().isEmpty()) {
            buildingEventVo.getParams().put("endTime", endTime);
        }

        startPage();
        List<BuildingEventVo> list = buildingEventService.selectBuildingEventList(buildingEventVo);
        return getDataTable(list);
    }

    /**
     * 获取楼栋告警详情（对应楼层下的所有告警事件）
     */
    @PreAuthorize("@ss.hasPermi('project:bevent:query')")
    @GetMapping("/details")
    public AjaxResult getEventDetails(@RequestParam Long locationId,
                                      @RequestParam(required = false) String deviceNo,
                                      @RequestParam(required = false) Integer chanel,
                                      @RequestParam(required = false) String beginTime,
                                      @RequestParam(required = false) String endTime,
                                      @RequestParam(required = false) Integer eventStatus) {
        List<PjAlarmEvent> eventDetails = buildingEventService.selectEventDetailsByLocation(
                locationId, deviceNo, chanel, beginTime, endTime, eventStatus);
        return success(eventDetails);
    }

    /**
     * 导出楼栋告警列表
     */
    @PreAuthorize("@ss.hasPermi('project:bevent:export')")
    @Log(title = "楼栋告警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuildingEventVo buildingEventVo) {
        List<BuildingEventVo> list = buildingEventService.selectBuildingEventList(buildingEventVo);
        // 使用ExcelUtil导出，需要适配BuildingEventVo
         ExcelUtil<BuildingEventVo> util = new ExcelUtil<BuildingEventVo>(BuildingEventVo.class);
         util.exportExcel(response, list, "楼栋告警数据");
    }
}